<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 指标信息管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.id" placeholder="作者编号" class="handle-input mr10" style="width: 100px"></el-input>
                <el-select v-model="query.sex" placeholder="性别" class="handle-select mr10" clearable>
                    <el-option key="1" label="男" value="1"></el-option>
                    <el-option key="2" label="女" value="0"></el-option>
                </el-select>
                <el-input v-model="query.name" placeholder="作者姓名" class="handle-input mr10" style="width: 150px"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>

                <el-button type="primary" icon="el-icon-download" @click="handleSearch">模板下载</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="handleSearch">导入</el-button>
                <el-button type="primary" icon="el-icon-minus" @click="handleSearch">导出</el-button>
            </div>
            <el-table :data="authorList" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="name" label="姓名"></el-table-column>
                <el-table-column label="体重">
                    <template slot-scope="scope">
                        {{ scope.row.sex == 1 ? '男' : '女' }}
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="体温"></el-table-column>
                <el-table-column prop="description" label="血压"></el-table-column>
                 <el-table-column prop="description" label="脉搏"></el-table-column>
                  <el-table-column prop="description" label="血糖"></el-table-column>
                   <el-table-column prop="description" label="总胆固醇"></el-table-column>
                <el-table-column label="尿酸" align="center">
                    <template slot-scope="scope">
                        <el-image class="table-td-thumb" :src="scope.row.headerUrl" :preview-src-list="[scope.row.headerUrl]"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <!-- <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)"
                            >删除</el-button
                        > -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="author" label-width="70px" :rules="ruleForm">
                <el-form-item label="编号">
                    <el-input v-model="author.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="author.name"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="author.sex" placeholder="性别" class="handle-select mr10" clearable>
                        <el-option key="1" label="男" value="1"></el-option>
                        <el-option key="2" label="女" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birthday">
                    <el-input v-model="author.birthday" type="date"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="description">
                    <el-input v-model="author.description" type="textarea" :rows="3"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit('form')">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                name: '',
                pageIndex: 1,
                id: '',
                sex: ''
            },
            authorList: [],
            author: {
                id: '',
                name: '',
                sex: 1,
                birthday: '',
                description: '',
                headerUrl: ''
            },
            ruleForm: {
                name: [{ required: true, message: '请输入作者姓名', trigger: 'blur' }],
                sex: [{ required: true, message: '请选择性别', trigger: 'change' }],
                birthday: [{ required: true, message: '请选择出生日期', trigger: 'change' }],
                description:[]
            },
            sex:'1',
            editVisible: false,
            pageSize: 10,
            pageTotal: 0,
            idx: -1,
        };
    },
    created() {
        this.handleSearch();
    },
    methods: {
        // 触发搜索按钮
        handleSearch() {
            this.$http.post('/author/query', this.query).then((res) => {
                this.authorList = res.data.data.list;
                this.pageTotal = res.data.data.total;
            });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.author = JSON.parse(JSON.stringify(row)); 
            this.author.sex = row.sex.toString();
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit(formName) {
            this.$refs[formName].validate((valid) => {
                this.$http.post("/author/update",this.author).then(res => {
                    if(res.data.status==1){
                        this.$set(this.authorList, this.idx, this.author);
                        this.$message.success(`修改 ${this.author.id} 号作者信息成功`);
                    }else{
                        this.$message.error(this.data.message);
                    }
                });
                
                this.editVisible = false;
            })
            
        },
        // 分页导航
        handlePageChange(val) {
            this.query.pageIndex = val;
            this.handleSearch();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
